﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BA2009.Web.Controllers;
using Moq;
using System.Web.Mvc;
using BA2009.Services;

namespace BA2009.Tests.Controllers
{
    [TestClass]
    public class PageControllerTest
    {
        PagesController CreatePagesController()
        {
            var controller = new PagesController(new PageService(new Fakes.FakePageRepository(Fakes.FakeBA2009Data.CreateTestPages())));

            return controller;
        }

        PagesController CreatePagesControllerAs(string username)
        {
            var mock = new Mock<ControllerContext>();

            //ControllerContext will return username
            mock.SetupGet(p => p.HttpContext.User.Identity.Name).Returns(username);

            //ControllerContext will return true when IsAuthenticated
            mock.SetupGet(p => p.HttpContext.Request.IsAuthenticated).Returns(true);

            var controller = CreatePagesController();
            controller.ControllerContext = mock.Object;
            return controller;
        
        }

        [TestMethod]
        public void details_action_should_return_view_for_page()
        {
            var controller = CreatePagesController();

            var result = controller.Details(1);

            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }

        [TestMethod]
        public void create_action_should_return_viewresult()
        {
            // Arrange
            var controller = CreatePagesController();
            // Act
            var result = controller.Create();

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }

        [TestMethod]
        public void create_action_should_return_page_model()
        {
            // Arrange
            var controller = CreatePagesController();

            // Act
            ViewResult result = (ViewResult)controller.Create();

            // Assert
            Assert.IsInstanceOfType(result.ViewData.Model, typeof(BA2009.Entities.Page));
        }

        [TestMethod]
        public void create_action_with_new_page_should_return_view_and_repo_should_contain_new_page()
        {

        

            var mock = new Mock<ControllerContext>();

            var testData = Fakes.FakeBA2009Data.CreateTestPages();
            var repository = new Fakes.FakePageRepository(testData);

            var service = new PageService(repository);
            var controller = new PagesController(service);

            var page = Fakes.FakeBA2009Data.CreatePage();

            //ActionResult result = (ActionResult)controller.Create(page);

            //Assert
            Assert.AreEqual(10, service.FindAll().Count);

            //Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
        }

        [TestMethod]
        public void edit_action_should_return_view_result()
        {
            // Arrange
            var controller = CreatePagesController();
            // Act
            var result = controller.Edit(1);

            // Assert
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }

        [TestMethod]
        public void edit_action_should_return_page_model()
        {
            // Arrange
            var controller = CreatePagesController();

            // Act
            ViewResult result = (ViewResult)controller.Edit(1);

            // Assert
            Assert.IsInstanceOfType(result.ViewData.Model, typeof(BA2009.Entities.Page));
        }

        [TestMethod]
        public void edit_action_with_new_page_should_return_view_and_repo_should_contain_new_page()
        {



            var mock = new Mock<ControllerContext>();

            var testData = Fakes.FakeBA2009Data.CreateTestPages();
            var repository = new Fakes.FakePageRepository(testData);

            var service = new PageService(repository);
            var controller = new PagesController(service);

            var page = Fakes.FakeBA2009Data.CreatePage();

            //ActionResult result = (ActionResult)controller.Edit(page);

            //Assert
            //Assert.AreEqual(10, service.FindAll().Count);

            //Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
        }



    }
}
